<template>
  <div class="article-list">
    <el-card class="box-card">
      <!-- 搜索工具栏 -->
      <div class="toolbar">
        <div class="search-box">
          <el-input
            v-model="queryParams.keyword"
            placeholder="请输入标题关键词"
            clearable
            style="width: 200px;"
            @keyup.enter="handleQuery"
          >
            <template #prefix>
              <el-icon><Search /></el-icon>
            </template>
          </el-input>
          <el-select v-model="queryParams.category" placeholder="文章类型" clearable>
            <el-option label="公告" value="NOTICE" />
            <el-option label="心理知识" value="KNOWLEDGE" />
          </el-select>
          <el-button type="primary" @click="handleQuery">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </div>
      </div>

      <!-- 文章列表 -->
      <el-table v-loading="loading" :data="articleList">
        <el-table-column label="标题" prop="title" show-overflow-tooltip>
          <template #default="{ row }">
            <el-link type="primary" :underline="false" @click="handleTitleClick(row)">
              {{ row.title }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="类型" prop="category" width="100">
          <template #default="{ row }">
            <el-tag :type="row.category === 'NOTICE' ? 'warning' : 'success'">
              {{ row.category === 'NOTICE' ? '公告' : '心理知识' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="重要" width="80">
          <template #default="{ row }">
            <el-tag v-if="row.isImportant" type="danger">重要</el-tag>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" width="180" />
        <el-table-column label="操作" align="center" width="100">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleView(row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="queryParams.pageNum"
          v-model:page-size="queryParams.pageSize"
          :total="articleList.length"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { Search, Refresh } from '@element-plus/icons-vue'
import { getArticleList } from '@/api/article'
import { useRouter } from 'vue-router'

const router = useRouter()

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  keyword: '',
  category: '',
  status: '1', // 只显示已发布的文章
  orderBy: 'order_num asc'
})

// 文章列表数据
const loading = ref(false)
const total = ref(0)
const articleList = ref<any[]>([])

// 查询方法
const handleQuery = async () => {
  loading.value = true
  try {
    const { data } = await getArticleList(queryParams)
    articleList.value = data.records
    total.value = data.total
  } catch (error) {
    console.error('查询失败:', error)
  } finally {
    loading.value = false
  }
}

// 重置查询
const resetQuery = () => {
  queryParams.keyword = ''
  queryParams.category = ''
  handleQuery()
}

// 分页方法
const handleSizeChange = (val: number) => {
  queryParams.pageSize = val
  handleQuery()
}

const handleCurrentChange = (val: number) => {
  queryParams.pageNum = val
  handleQuery()
}

// 标题点击事件
const handleTitleClick = (row: any) => {
  router.push(`/notice/detail/${row.id}`)
}

// 查看按钮点击事件
const handleView = (row: any) => {
  router.push(`/notice/detail/${row.id}`)
}

// 初始化
handleQuery()
</script>

<style lang="scss" scoped>
.article-list {
  .toolbar {
    margin-bottom: 20px;

    .search-box {
      display: flex;
      gap: 12px;
      flex-wrap: wrap;

      .el-select {
        width: 120px;
      }
    }
  }

  .pagination {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
}
</style> 